/*需要您手动修改baas代码，没有了解baas的请先了解baas的原理！然后参照apache-tomcat\webapps\baas\src\com\justep\templates\Test.java
修改数据库的库名和表名即可
另外需要修改：
	配置数据源：apache-tomcat\webapps\baas\META-INF\context.xml
				（修改时手动同步：apache-tomcat\conf\Catalina\localhost\baas.xml）
	映射		：apache-tomcat\webapps\baas\WEB-INF\web.xml 
	url     :下边代码中的Baas.sendRequest方法中的url要和web.xml中的配置一致
*/


define(function(require){
	var $ = require("jquery");
	var justep = require("$UI/system/lib/justep");
    var check = require("$UI/communityOto/common/check");
    
	
	var Model = function(){
	    this.isSearchMode = false;
	    this.timer = 0;
	    this.communityListShow = false;
		this.callParent();
	};
	
	Model.prototype.registerClick = function(event){
	      
	  
		var  registerData = this.comp("registData");
		var  formData = new FormData();
         
         console.log(registerData);
		
		
		if(registerData.val("company.id")&&registerData.val("name")&&registerData.val("password")&&registerData.val("loginName")){
			
			
			formData.append("company.id",registerData.val("company.id"));
		    formData.append("name",registerData.val("name"));
		    formData.append("password",registerData.val("password"));
		    formData.append("loginName",registerData.val("loginName"));
		    if(registerData.val("sex")){
		    	formData.append("sex",registerData.val("id"));
		    }
		    
			if(registerData.val("email")){
				formData.append("email",registerData.val("email"));
			}
			
			if(registerData.val("phone")){
				formData.append("phone",registerData.val("phone"));
			}
			
			if(registerData.val("mobile")){
			
				formData.append("mobile",registerData.val("mobile"));
			}
			
			
			if(document.getElementById(this.getIDByXID("photoFile")).files[0]){
			   
				formData.append("image",document.getElementById(this.getIDByXID("photoFile")).files[0]);
			}
			
			if(registerData.val("office.id")){
			   
				formData.append("office.id",registerData.val("office.id"));
			}
			
			var registerUrl = "http://www.xjzlhy.com:81/ex_community/a/api/community/register";
				$.ajax({
				  url: registerUrl,
				  type: "POST",
				  data: formData,
				  processData: false,  // 告诉jQuery不要去处理发送的数据
				  contentType: false,   // 告诉jQuery不要去设置Content-Type请求头
				  success:function(result){
				       
				        if(result.errNo == -1){
				        	justep.Util.hint("该用户已注册", {type:"danger"});
				        }else{
				        	justep.Util.hint("注册成功", {type:"success"});
		  				    justep.Shell.showPage("main");
				        }
		  							
			      }
				});
		}else{
			justep.Util.hint("请补充完整必填信息", {type:"danger"});
		}
	};
	
	Model.prototype.communityDataCustomRefresh = function(event){
	    var url = "http://www.xjzlhy.com:81/ex_community/a/api/community/getAllCommunity?callback=?";
		$.getJSON(
				url,
				function(data){
					event.source.loadData(data.data);
				}
		
		);

	};
	
	
	
	Model.prototype.addPhotoClick = function(event){
		var fileId = this.getIDByXID("photoFile");
		$("#"+fileId).click();
	};
	
	
	
	Model.prototype.photoFileChange = function(event){
		var fileList = event.currentTarget.files;
        $(".userImg").attr("src",window.URL.createObjectURL(fileList[0]));
	};
	Model.prototype.loginNameBlur = function(event){
	  if(!check.testUserName($(event.currentTarget).val())){
		  justep.Util.hint("请输入以字母开头的5-15个字母、数字或下划线",{type:"danger"});
	  }

	};
	Model.prototype.nameBlur = function(event){
		if(!check.testName($(event.currentTarget).val())){
		
		     justep.Util.hint("请输入2个以上的汉子", {type:"danger"});
		}
	};
	Model.prototype.passwordBlur = function(event){
		if(!check.testPassword($(event.currentTarget).val())){
			justep.Util.hint("请输入以字母开头的6-18个字母、数字或下划线",{type:"danger"});
		}
	};
	Model.prototype.emailBlur = function(event){
		
		if(!check.testEmail($(event.currentTarget).val())){
			justep.Util.hint("请输入正确的邮箱格式",{type:"danger"});
		}
	};
	Model.prototype.phoneBlur = function(event){
		if(!check.testPhone($(event.currentTarget).val())){
			justep.Util.hint("请输入正确的电话号码格式",{type:"danger"});
		}

	};
	Model.prototype.mobileBlur = function(event){
		if(!check.testMobile($(event.currentTarget).val())){
			justep.Util.hint("请输入正确的手机号码格式",{type:"danger"});
		}

	};
	Model.prototype.input1Focus = function(event){
	    $("#"+this.getIDByXID("list2")).hide();
	    this.communityListShow = false;
		var me = this;
		this.timer = setInterval(function() {
		    me.comp("searchCommunityData").clear();
			var searchVal = me.comp("input1").val();
			// console.log('test',searchVal);
			if (searchVal) {
                
				var result = me.comp("communityData").find([ 'name' ], [ searchVal ], false, true, true);
                if (result.length > 0) {

					for (var i = 0; i < result.length; i++) {
						me.comp("searchCommunityData").newData({
							defaultValues : [ {
								'id' : result[i].val('id'),
								'name' : result[i].val('name')
							} ]
						});

					}
					me.comp("list1").refresh();
					// this.comp("searchAreaData").first();
					// this.comp("searchAreaData").refreshData(true);
				}
				
			}
		}, 1000);

	};
	

	
	Model.prototype.input1Change = function(event){


        if(this.timer ){
            console.log('ok');
        	clearInterval(this.timer);
        }

	};
	//选择社区事件	
	Model.prototype.list1Click = function(event){
		var currentRow = this.comp("searchCommunityData").getCurrentRow();
		$('#'+this.getIDByXID("input1")).val(currentRow.val('name'));
		this.comp("registData").getFirstRow().val('company.id',currentRow.val('id'));
		this.comp("searchCommunityData").clear();
		clearInterval(this.timer);
		
		var url = "http://www.xjzlhy.com:81/ex_community/a/api/community/findChildById?callback=?";
		var me=this;
		$.getJSON(url,{"id":currentRow.val('id')},function(data){
			me.comp("areaData").loadData(data.data);
		});

	};
	
	Model.prototype.input1Blur = function(event){
		 if(this.timer ){
            
        	clearInterval(this.timer);
        }
	};
	
	
	Model.prototype.col3Click = function(event){
			if(this.isSearchMode){
				$('.communitySearch').hide();
				$('.communitySelect').show();
				$('.changeModeBtn').text("搜索");
				this.isSearchMode = false;
			}else{
				$('.communitySearch').show();
				$('.communitySelect').hide();
				$('.changeModeBtn').text("选择");
				this.isSearchMode = true;
			}
	};
	
	Model.prototype.select1Change = function(event){
		var url = "http://www.xjzlhy.com:81/ex_community/a/api/community/findChildById?callback=?";
		var me = this;
		var communityId=this.comp("registData").getFirstRow().val("company.id");
		console.log(communityId);
		$.getJSON(url,{"id":communityId},function(data){
		    console.log(data);
			me.comp("areaData").loadData(data.data);
		});

	};
	
	Model.prototype.button2Click = function(event){
	    if(this.communityListShow){
	    	$("#"+this.getIDByXID("list2")).hide();
	    	this.communityListShow = false;
	    	
	    }else{
	    	$("#"+this.getIDByXID("list2")).show();
	    	this.communityListShow = true;
	    }
		
	};
	
	Model.prototype.list2Click = function(event){
		var currentRow = this.comp("communityData").getCurrentRow();
		$('#'+this.getIDByXID("input1")).val(currentRow.val('name'));
		this.comp("registData").getFirstRow().val('company.id',currentRow.val('id'));
		$("#"+this.getIDByXID("list2")).hide();
		this.communityListShow = false;
		
		var url = "http://www.xjzlhy.com:81/ex_community/a/api/community/findChildById?callback=?";
		var me=this;
		$.getJSON(url,{"id":currentRow.val('id')},function(data){
			me.comp("areaData").loadData(data.data);
		});
		

	};
	
	return Model;
});